<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/10/13
 * Time: 10:08
 */

namespace app\api\controller;


use language\Languagemsg;
use think\Db;

class Bank extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     * 添加银行卡
     * @return \think\response\Json
     * bc_user_name        持卡人
     * bc_code             卡号
     * bc_bank_name        银行名称
     * bc_card_type        银行卡类型
     * bc_phone            手机号
     * bc_safe_code        安全码
     * bc_surname          姓
     * bc_name             名
     * bc_addr             地区信息
     * bc_address          详细地址
     * bc_call             电话
     * bc_email            邮箱
     * bc_country_type     1 中国卡    2 其他国家卡
     * bc_lose_time        国外卡有效期
     * code                验证码
     */
    public function addBankCard(){
        $bc_user_name = input('bc_user_name');
        $bc_code = input('bc_code');
        $bc_bank_name = input('bc_bank_name');
        $bc_card_type = input('bc_card_type');
        $bc_phone = input('bc_phone');
        $bc_safe_code = input('bc_safe_code');
        $bc_surname = input('bc_surname');
        $bc_name = input('bc_name');
        $bc_addr = input('bc_addr');
        $bc_address = input('bc_address');
        $bc_email = input('bc_email');
        $bc_call = input('bc_call');
        $bc_country_type = input('bc_country_type');
        $code = input('code');
        $bc_lose_time = input('bc_lose_time');
        
        $insert = [
            'bc_user_id' => $this->user_id,
            'bc_code' => $bc_code,
            'bc_user_name' => $bc_user_name,
            'bc_country_type' => $bc_country_type,
            'bc_card_type' => $bc_card_type,
            'bc_time' => $this->create_time
        ];
        if($bc_country_type == 1){ // 中国
            // 验证码
            $yzm = Db::table('up_phonecode')->where('phone', $bc_phone)->where('check_type', 1)->order('time desc')->limit(1)->find();
//            if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
            if($code != $yzm['code']) return json(['status' => 0, 'msg' => $this->msg_language->yzmErr[$this->language]]);
            // 5分钟超时
            $now = time();
//            if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => '验证码失效']);
//            if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
            if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->msg_language->yzmLose[$this->language]]);
            if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->yzmUsed[$this->language]]);
            
            $insert['bc_bank_name'] = $bc_bank_name;
            $insert['bc_phone'] = $bc_phone;
        }else{
            $insert['bc_safe_code'] = $bc_safe_code;
            $insert['bc_surname'] = $bc_surname;
            $insert['bc_name'] = $bc_name;
            $insert['bc_addr'] = $bc_addr;
            $insert['bc_address'] = $bc_address;
            $insert['bc_call'] = $bc_call;
            $insert['bc_email'] = $bc_email;
            $insert['bc_lose_time'] = $bc_lose_time;
        }
        Db::startTrans();
        try{
            // 更新验证码状态
            if($bc_country_type == 1){
                Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            }
            Db::table('up_bank_card')->insert($insert);
            Db::commit();
//            return json(['status' => 200, 'msg' => '添加成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->addSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 银行卡列表
     * @return \think\response\Json
     */
    public function cardList(){
        $list = Db::table('up_bank_card')
            ->where('bc_user_id', $this->user_id)
            ->where('bc_del', 1)
            ->field('bc_id, bc_code, bc_bank_name, bc_card_type, bc_country_type, bc_is_default, bc_lose_time')
            ->order('bc_time desc')
            ->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除银行卡
     * @return \think\response\Json
     * bc_id      记录ID
     */
    public function cardDel(){
        $bc_id = input('bc_id');
        $res = Db::table('up_bank_card')->where('bc_id', $bc_id)->setField('bc_del', 2);
        if($res){
//            return json(['status' => 200, 'msg' => '删除成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->delSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 设置默认银行卡
     * @return \think\response\Json
     * bc_id     银行卡ID
     */
    public function setDefault(){
        $bc_id = input('bc_id');
        Db::startTrans();
        try{
            // 先将所有的银行卡设为非默认
            Db::table('up_bank_card')
                ->where('bc_user_id', $this->user_id)
                ->where('bc_is_default', 1)
                ->setField('bc_is_default', 2);
            // 设置所选的银行卡为默认
            Db::table('up_bank_card')->where('bc_id', $bc_id)->setField('bc_is_default', 1);
            Db::commit();
//            return json(['status' => 200, 'msg' => '设置成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->setSuccess[$this->language]]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    public function getCardInfo(){
        // TODO:根据卡号，调取第三方接口，获取银行卡信息
        
    }
}